/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-20 17:21:24
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-27 11:56:47
 * @FilePath: \gogamewebserver\goGameWebServer\internal\controller\admin\admin_v1.go
 * @Description: 文件功能描述
 */
package admin

import (
	"context"
	v1 "goGameWebServer/api/admin/v1"
	"goGameWebServer/internal/common/config"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/controller"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/entityName"
	"goGameWebServer/internal/model/entity"
	"goGameWebServer/internal/register"

	"github.com/gogf/gf/v2/net/ghttp"
)

func (c *AdminControllerV1) QueryUser(ctx context.Context, req *v1.AdminQueryUserReq) (res *v1.AdminQueryUserRes, err error) {
	// 查询账号信息
	ac := register.GetModel(entityName.EntityName_Account)
	acData := ac.GetEntityData().(*entity.AccountModelData)
	acData.AccountName = req.AccountName
	ac.LoadEntityDataFromDB(nil)
	res = &v1.AdminQueryUserRes{}
	if acData.AvatarId > 0 {
		// 查询角色信息
		avc := register.GetModel(entityName.EntityName_Avatar)
		avcData := avc.GetEntityData().(*entity.AvatarModelData)
		avcData.Id = acData.AvatarId
		avc.LoadEntityDataFromDB(nil)
		decoded, e := utils.ZlibDecode(avcData.Data)
		if e != nil {
			decoded = ""
		}
		res.Data = decoded
		res.Name = avcData.Name
		res.UpdateTime = avcData.LastTime
	}
	res.AccountId = acData.Id
	res.AccountName = acData.AccountName
	res.Id = acData.AvatarId
	res.CreateTime = acData.CreateTime
	controller.BackResponseOK(ghttp.RequestFromCtx(ctx), res)
	return
}

func (c *AdminControllerV1) UpdateUser(ctx context.Context, req *v1.AdminUpdateUserReq) (res *v1.AdminUpdateUserRes, err error) {
	ac := register.GetModel(entityName.EntityName_Account)
	acData := ac.GetEntityData().(*entity.AccountModelData)
	acData.AccountName = req.AccountName
	ac.LoadEntityDataFromDB(nil)
	// 比对accountId
	if acData.Id != req.AccountId {
		controller.BackResponseError(ghttp.RequestFromCtx(ctx), cerror.ADMIN_ACCOUNT_ID_ERROR,
			cerror.GetErrMsg(cerror.ADMIN_ACCOUNT_ID_ERROR))
		return
	}
	if acData.AvatarId <= 0 || acData.AvatarId != req.Id {
		controller.BackResponseError(ghttp.RequestFromCtx(ctx), cerror.ADMIN_AVATAR_ID_ERROR,
			cerror.GetErrMsg(cerror.ADMIN_AVATAR_ID_ERROR))
		return
	}
	// 查询角色信息
	avc := register.GetModel(entityName.EntityName_Avatar)
	avcData := avc.GetEntityData().(*entity.AvatarModelData)
	avcData.Id = acData.AvatarId
	avc.LoadEntityDataFromDB(nil)
	updateData, e := utils.ZlibEncode(req.Data)
	if e != nil {
		controller.BackResponseError(ghttp.RequestFromCtx(ctx), cerror.ADMIN_DATA_ENCODE_ERROR,
			cerror.GetErrMsg(cerror.ADMIN_DATA_ENCODE_ERROR))
		return
	}
	avcData.Data = updateData
	avc.SaveEntityDataToDB()
	res = &v1.AdminUpdateUserRes{}
	res.AccountId = acData.Id
	res.AccountName = acData.AccountName
	res.Id = acData.AvatarId
	res.CreateTime = acData.CreateTime
	res.UpdateTime = avcData.LastTime
	controller.BackResponseOK(ghttp.RequestFromCtx(ctx), res)
	return
}

func (c *AdminControllerV1) UpdateAvatar(ctx context.Context, req *v1.AdminUpdateAvatarReq) (res *v1.AdminUpdateAvatarRes, err error) {
	ac := register.GetModel(entityName.EntityName_Account)
	acData := ac.GetEntityData().(*entity.AccountModelData)
	acData.AccountName = req.AccountName
	e := ac.LoadEntityDataFromDB(nil)
	if e != nil {
		controller.BackResponseError(ghttp.RequestFromCtx(ctx), cerror.HTTP_ACCOUNT_ERROR,
			cerror.GetErrMsg(cerror.HTTP_ACCOUNT_ERROR))
		return
	}
	acData.Password = req.PassWord
	acData.ThirdPlatformType = req.ThirdPlatformType
	acData.ThirdPlatform = req.ThirdPlatform
	acData.ThirdAccount = req.ThirdAccount
	ac.SaveEntityDataToDB()
	if acData.AvatarId > 0 {
		// 查询角色信息
		avc := register.GetModel(entityName.EntityName_Avatar)
		avcData := avc.GetEntityData().(*entity.AvatarModelData)
		updateData, e := utils.ZlibEncode(req.AvatarData)
		if e != nil {
			controller.BackResponseError(ghttp.RequestFromCtx(ctx), cerror.ADMIN_DATA_ENCODE_ERROR,
				cerror.GetErrMsg(cerror.ADMIN_DATA_ENCODE_ERROR))
			return
		}
		avcData.Data = updateData
	}
	controller.BackResponseOK(ghttp.RequestFromCtx(ctx), nil)
	return
}

func (c *AdminControllerV1) UpdateGameConfig(ctx context.Context, req *v1.AdminUpdateGameConfigReq) (res *v1.AdminUpdateGameConfigRes, err error) {
	// 清除所有配置
	if err = entity.ClearAllConfigs(); err != nil {
		return
	}
	// TODO: 后续可在此处应用 req.Configs 内容到数据库
	for _, v := range req.Configs {
		cc := register.GetModel(entityName.EntityName_GameConfig)
		ccData := cc.GetEntityData().(*entity.GameConfigModelData)
		ccData.Name = v.Name
		ccData.Type = v.Type
		ccData.IsActive = v.IsActive
		ccData.Platform = v.Platform
		ccData.ParentId = v.ParentId
		ccData.Value = v.Value
		cc.SaveEntityDataToDB()
	}
	res = &v1.AdminUpdateGameConfigRes{}
	controller.BackResponseOK(ghttp.RequestFromCtx(ctx), res)
	return
}

func (c *AdminControllerV1) UpdateServerKey(ctx context.Context, req *v1.AdminUpdateServerKeyReq) (res *v1.AdminUpdateServerKeyRes, err error) {
	if req.Secret == "" {
		controller.BackResponseError(ghttp.RequestFromCtx(ctx), cerror.ADMIN_SERVER_KEY_EMPTY_ERROR,
			cerror.GetErrMsg(cerror.ADMIN_SERVER_KEY_EMPTY_ERROR))
		return
	}
	serverM := register.GetModel(entityName.EntityName_Server)
	serverMData := serverM.GetEntityData().(*entity.ServerModelData)
	serverMData.Zone = config.AppOptions.ServerID
	serverM.LoadEntityDataFromDB(nil)
	serverMData.WebSecret = req.Secret
	serverM.SaveEntityDataToDB()
	config.AppOptions.ServerKey = req.Secret
	res = &v1.AdminUpdateServerKeyRes{}
	controller.BackResponseOK(ghttp.RequestFromCtx(ctx), res)
	return
}
